/*
 * @Author: your name
 * @Date: 2021-08-08 11:09:55
 * @LastEditTime: 2021-09-09 21:57:26
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /blogServe/src/app/error-handle.js
 */
const errorTypes = require('../constants/error-types');

const errorHandle = (error, ctx) => {
  let status, message;
  switch (error.message) {
    case errorTypes.NAME_OR_PASSWORD_IS_REQUIRED:
      status = 400; //Bad request
      message = "用户名或者密码错误"
      break;
    case errorTypes.USER_ALREADY_EXISTS:
      status = 409;
      message = "用户名重复，请重新输入";
      break;
    case errorTypes.USER_DOES_NOT_EXISTS:
      status = 400; // 参数错误
      message = "用户不存在";
      break;
    case errorTypes.PASSWORD_IS_INCORRENT:
      status = 400;
      message = "密码不正确的";
      break;
    case errorTypes.UNAUTHORIZATION:
      status = 401;
      meassge = "无效的token";
      break;
    case errorTypes.UNPERMISSION:
      status = 401;
      meassge = "您不具备操作权限~";
      break;
    default: 
      status = 404;
      message = "NOT FOUND"
  }
  ctx.status = status;
  ctx.body = message;
}

module.exports = errorHandle;